<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:alarm:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:alarm:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:alarm:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:alarm:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="alarmList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="snmp" align="center" prop="snmpCrisis" />
      <el-table-column label="smtp" align="center" prop="smtpCrisis" />
      <el-table-column label="windows事件日志" align="center" prop="windowsLogCrisis" />
      <el-table-column label="snmp" align="center" prop="snmpAlarm" />
      <el-table-column label="smtp" align="center" prop="smtpAlarm" />
      <el-table-column label="windows事件日志" align="center" prop="windowsLogAlarm" />
      <el-table-column label="snmp" align="center" prop="snmpNotice" />
      <el-table-column label="smtp" align="center" prop="smtpNotice" />
      <el-table-column label="windows事件日志" align="center" prop="windowsLogNotice" />
      <el-table-column label="snmp" align="center" prop="snmpUnknown" />
      <el-table-column label="smtp" align="center" prop="smtpUnknown" />
      <el-table-column label="windows事件日志" align="center" prop="windowsLogUnknown" />
      <el-table-column label="异常等级" align="center" prop="abnormalLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.abnormal_level" :value="scope.row.abnormalLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="异常状态" align="center" prop="abnormalStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.abnormal_status" :value="scope.row.abnormalStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="异常采集告警状态" align="center" prop="alarmStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.alarm_status" :value="scope.row.alarmStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:alarm:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:alarm:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改异常采集告警对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="42%" append-to-body>
        <div style="height: 500px;overflow: auto;">
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="snmp" prop="snmpCrisis">
          <el-input v-model="form.snmpCrisis" placeholder="请输入snmp" />
        </el-form-item>
        <el-form-item label="smtp" prop="smtpCrisis">
          <el-input v-model="form.smtpCrisis" placeholder="请输入smtp" />
        </el-form-item>
        <el-form-item label="windows事件日志" prop="windowsLogCrisis">
          <el-input v-model="form.windowsLogCrisis" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="snmp" prop="snmpAlarm">
          <el-input v-model="form.snmpAlarm" placeholder="请输入snmp" />
        </el-form-item>
        <el-form-item label="smtp" prop="smtpAlarm">
          <el-input v-model="form.smtpAlarm" placeholder="请输入smtp" />
        </el-form-item>
        <el-form-item label="windows事件日志" prop="windowsLogAlarm">
          <el-input v-model="form.windowsLogAlarm" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="snmp" prop="snmpNotice">
          <el-input v-model="form.snmpNotice" placeholder="请输入snmp" />
        </el-form-item>
        <el-form-item label="smtp" prop="smtpNotice">
          <el-input v-model="form.smtpNotice" placeholder="请输入smtp" />
        </el-form-item>
        <el-form-item label="windows事件日志" prop="windowsLogNotice">
          <el-input v-model="form.windowsLogNotice" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="snmp" prop="snmpUnknown">
          <el-input v-model="form.snmpUnknown" placeholder="请输入snmp" />
        </el-form-item>
        <el-form-item label="smtp" prop="smtpUnknown">
          <el-input v-model="form.smtpUnknown" placeholder="请输入smtp" />
        </el-form-item>
        <el-form-item label="windows事件日志" prop="windowsLogUnknown">
          <el-input v-model="form.windowsLogUnknown" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="异常等级" prop="abnormalLevel">
          <el-select v-model="form.abnormalLevel" placeholder="请选择异常等级">
            <el-option
              v-for="dict in dict.type.abnormal_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="异常状态" prop="abnormalStatus">
          <el-select v-model="form.abnormalStatus" placeholder="请选择异常状态">
            <el-option
              v-for="dict in dict.type.abnormal_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="异常采集告警状态" prop="alarmStatus">
          <el-select v-model="form.alarmStatus" placeholder="请选择异常采集告警状态">
            <el-option
              v-for="dict in dict.type.alarm_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入是否删除" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="扩展1" prop="extend1">
          <el-input v-model="form.extend1" placeholder="请输入扩展1" />
        </el-form-item>
        <el-form-item label="扩展2" prop="extend2">
          <el-input v-model="form.extend2" placeholder="请输入扩展2" />
        </el-form-item>
        <el-form-item label="扩展3" prop="extend3">
          <el-input v-model="form.extend3" placeholder="请输入扩展3" />
        </el-form-item>
        <el-form-item label="扩展4" prop="extend4">
          <el-input v-model="form.extend4" placeholder="请输入扩展4" />
        </el-form-item>
        <el-form-item label="扩展5" prop="extend5">
          <el-input v-model="form.extend5" placeholder="请输入扩展5" />
        </el-form-item>
        <el-form-item label="扩展6" prop="extend6">
          <el-input v-model="form.extend6" placeholder="请输入扩展6" />
        </el-form-item>
        <el-form-item label="扩展7" prop="extend7">
          <el-input v-model="form.extend7" placeholder="请输入扩展7" />
        </el-form-item>
        <el-form-item label="扩展8" prop="extend8">
          <el-input v-model="form.extend8" placeholder="请输入扩展8" />
        </el-form-item>
        <el-form-item label="扩展9" prop="extend9">
          <el-input v-model="form.extend9" placeholder="请输入扩展9" />
        </el-form-item>
        <el-form-item label="扩展10" prop="extend10">
          <el-input v-model="form.extend10" placeholder="请输入扩展10" />
        </el-form-item>
      </el-form>
        </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAlarm, getAlarm, delAlarm, addAlarm, updateAlarm } from "@/api/system/alarm";

export default {
  name: "Alarm",
  dicts: ['abnormal_level', 'alarm_status', 'abnormal_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 异常采集告警表格数据
      alarmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询异常采集告警列表 */
    getList() {
      this.loading = true;
      listAlarm(this.queryParams).then(response => {
        this.alarmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        snmpCrisis: null,
        smtpCrisis: null,
        windowsLogCrisis: null,
        snmpAlarm: null,
        smtpAlarm: null,
        windowsLogAlarm: null,
        snmpNotice: null,
        smtpNotice: null,
        windowsLogNotice: null,
        snmpUnknown: null,
        smtpUnknown: null,
        windowsLogUnknown: null,
        abnormalLevel: null,
        abnormalStatus: null,
        alarmStatus: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        extend1: null,
        extend2: null,
        extend3: null,
        extend4: null,
        extend5: null,
        extend6: null,
        extend7: null,
        extend8: null,
        extend9: null,
        extend10: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加异常采集告警";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAlarm(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改异常采集告警";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAlarm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAlarm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除异常采集告警编号为"' + ids + '"的数据项？').then(function() {
        return delAlarm(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/alarm/export', {
        ...this.queryParams
      }, `alarm_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
